<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>代理商管理</title>
	<meta http-equiv="Access-Control-Allow-Origin" content="*">
	<meta name="decorator" content="default"/>

	<script type="text/javascript" src="${ctxStatic}/common/jquery-2.0.0.min.js"></script>

	<script type="text/javascript" src="${ctxStatic}/bower_components/moxie.js"></script>
	<script type="text/javascript" src="${ctxStatic}/bower_components/plupload.dev.js"></script>
	<%--<script type="text/javascript" src="/static/bower_components/plupload/js/plupload.full.min.js"></script>--%>
	<script type="text/javascript" src="${ctxStatic}/bower_components/qiniu.js"></script>
	<script type="text/javascript" src="${ctxStatic}/jquery-validation/1.11.0/jquery.validate.min.js"></script>
	<script type="text/javascript" src="${ctxStatic}/ckeditor/ckeditor.js"></script>
	<link href="${ctxStatic}/common/mycss.css" rel="stylesheet" type="css">
	<script type="text/javascript" src="${ctxStatic}/supcan/ajaxSet.js"></script>
	<script type="text/javascript">
		function passwordLevel(password) {
			var Modes = 0;
			for (i = 0; i < password.length; i++) {
				Modes |= CharMode(password.charCodeAt(i));
			}
			return bitTotal(Modes);
			//CharMode函数
			function CharMode(iN) {
				if (iN >= 48 && iN <= 57)//数字
					return 1;
				if (iN >= 65 && iN <= 90) //大写字母
					return 2;
				if ((iN >= 97 && iN <= 122) || (iN >= 65 && iN <= 90))
				//大小写
					return 4;
				else
					return 8; //特殊字符
			}
			//bitTotal函数
			function bitTotal(num) {
				modes = 0;
				for (i = 0; i < 4; i++) {
					if (num & 1) modes++;
					num >>>= 1;
				}
				return modes;
			}
		}
		$(document).ready(function() {
			$.validator.addMethod("strongPsw",function(value,element){
				if(passwordLevel(value)==1){
					return false;
				}else{
					return true
				}
			},"请输入数字+字母大小写混合密码");
			$("#no").focus();
			$("#inputForm").validate({
				rules: {
					loginName: {remote: "${ctx}/sys/user/checkLoginName?oldLoginName=" + encodeURIComponent('${user.loginName}')},
					newPassword:{
						required:true, //必填
						minlength:6, //最小长度
						maxlength:32, //最大长度
						strongPsw:true, //密码强度
					},
					mobile:{
						required:true,
					},
					confirmNewPassword:{
						required:true,
						minlength:6,
						maxlength:32,
						equalTo:"#newPassword" //再次填写密码需一致
					}
				},
				messages: {
					loginName: {remote: "用户登录名已存在"},
					confirmNewPassword: {equalTo: "输入与上面相同的密码"}
				},
				submitHandler: function(form){
					loading('正在提交，请稍等...');
					form.submit();
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
			});

		});
		function showAgentList(citysId){
			$.ajax({
				type : "POST",
				url : "${ctx}/sys/office/showAgentListByCityId",
				data : {
					citysId : citysId
				},
				dataType : "JSON",
				success : function(data) {
					var str = "<option>请选择</option>";
					$("#agentId").html("");
					for ( var i = 0; i < data.length; i++) {
						str += "<option value='"+data[i].id+"'>" + data[i].name + "</option>";
					}
					$("#agentId").append(str);
				}
			});
		}
		function showCommunityList(areasId){
			$.ajax({
				type : "POST",
				url : "${ctx}/sys/office/showCommunityListByAreaId",
				data : {
					areasId : areasId
				},
				dataType : "JSON",
				success : function(data) {
					var str = "<option>请选择</option>";
					$("#communityId").html("");
					for ( var i = 0; i < data.length; i++) {
						str += "<option value='"+data[i].id+"'>" + data[i].name + "</option>";
					}
					$("#communityId").append(str);
				}
			});
		}
	</script>
	<script type="text/javascript">
		var isok=0;
		$(function() {
			//将省id传入后台
			$("#box").change(function() {
				var pid = $("#box").val();
				$.ajax({
					type : "POST",
					url : "${ctx}/sys/office/showCityListByPid",
					data : {
						pid : pid
					},
					dataType : "JSON",
					success : function(date) {
						var str = "<option>请选择</option>";
						$("#box1").html("");
						for ( var i = 0; i < date.length; i++) {
							str += "<option value='"+date[i].cityid+"'>" + date[i].city + "</option>";

						}
						$("#box1").append(str);
						$("#box").change(function() {
							$("#box1").val("请选择");
							$("#box2").val("请选择");
						});

					}
				});

			});


			//将城市id传入后台
		/*	$("#box1").change(function() {
				var cid = $("#box1").val();
				$("#cityId").val($("#box1 option:selected").val());
				$("#cityName").val($("#box1 option:selected").text());
				$.ajax({
					type : "POST",
					url : "${ctx}/sys/office/showTownListByCid",
					data : {
						cid : cid
					},
					dataType : "JSON",
					success : function(date) {
						var str = "<option>请选择</option>";
						$("#box2").html("");
						for ( var i = 0; i < date.length; i++) {
							str += "<option value='"+date[i].areaid+"'>" + date[i].area + "</option>";

						}
						$("#box2").append(str);
						$("#box1").change(function() {
							$("#box2").find("option").remove();
						});

					}

				});

			});*/
			//判断代理商是否是这个区域唯一的
			$("#box1").change(function() {
				var provid=$("#box").val();
				var cityid=$("#box1").val();
					$.ajax({
						type : "POST",
						url : "${ctx}/sys/agent/checkAgentAreas",
						data : "pid="+provid+"&cityid="+cityid,
						success : function(date) {
							if(date=='0'){
								isok=1;
								var spa=document.getElementById("agent");
								spa.innerHTML="*";
							}else {
								isok=2;
								var spa=document.getElementById("agent");
								spa.innerHTML="该市已经有了代理商，请更换城市！";
								//$("#box1").find("option").remove();


							}
						}

					});

			});
			$("#btnSubmit").click(function(){
				if(isok!=2){
					$("#inputForm").submit();
				}
			});
		});
	</script>
	<script>
		$(document).ready(function () {
			$(document).on("click", "A[name='deleteImg']", function () {
				$(this).parent().remove();
			});
			//$("#value").focus();
		});
	</script>
	<script>
		$(function () {
			var uploader = Qiniu.uploader({
				runtimes: 'html5,flash,html4',
				browse_button: 'pickfiles',
				container: 'container',
				drop_element: 'container',
				max_file_size: '1000mb',
				flash_swf_url: 'bower_components/plupload/js/Moxie.swf',
				dragdrop: true,
				chunk_size: '4mb',
				multi_selection: !(mOxie.Env.OS.toLowerCase() === "ios"),
				// uptoken_url: '/a/qiniu/upload/token',http://localhost:8080/a/qiniu/upload/token"
				uptoken_url: '/a/qiniu/upload/token',
				domain: "oframjnn6.bkt.clouddn.com",
				get_new_uptoken: false,
				auto_start: true,
				log_level: 5,
				unique_names: true,
				init: {
					'FilesAdded': function (up, files) {
						plupload.each(files, function (file) {
						});
					},
					'BeforeUpload': function (up, file) {
					},
					'UploadProgress': function (up, file) {
						console.log(up);
					},
					'UploadComplete': function () {
					},
					'FileUploaded': function (up, file, info) {
					},
					'Error': function (up, err, errTip) {

					}
				}
			});

			uploader.bind('FileUploaded', function (up, file, info) {

				console.log("resp_URL : " + JSON.parse(info.response).key);
				var _url = 'http://oframjnn6.bkt.clouddn.com/' + JSON.parse(info.response).key;
				var _html = '<li><img src="'+_url+'" ' +
						'url="'+_url+'" ' +
						'style="max-width:100px;max-height:100px;_height:100px;border:0;padding:3px;"> ' +
						'<input type="hidden" name="photo" value="'+_url+'"> ' +
						'&nbsp;&nbsp; ' +
						'<a href="javascript:void(0)" name="deleteImg">×</a> ' +
						'<input type="hidden" value="'+_url+'" class="input-text form-control"/></li>';
				console.log(_html);
				$("#nameImagePreview").html(_html);
			});

		});
	</script>
</head>
<body>
<ul class="nav nav-tabs">
	<li ><a href="${ctx}/sys/agent/agentAddList">代理商列表</a></li>
	<li class="active"><a href="${ctx}/sys/agent/form">代理商<shiro:hasPermission name="sys:user:edit">${not empty user.id?'修改':'添加'}</shiro:hasPermission>
	</a>
	</li>
</ul>
<br/>
	<form:form id="inputForm" modelAttribute="user" action="${ctx}/sys/agent/save" method="post" class="form-horizontal" enctype="multipart/form-data">
		<form:hidden path="id"/>
		<sys:message content="${message}"/>
		<div class="control-group">
			<label id="container" class="control-label"><a class="btn btn-default btn-lg " id="pickfiles" href="#">
				<i class="glyphicon glyphicon-plus"></i>
				<span>头像</span>
			</a>:</label>
			<div class="controls">
				<ol id="nameImagePreview">
					<c:if test="${user.photo!=null}">
						<li><img src="${user.photo}"
								 url="${user.photo}"
								 style="max-width:100px;max-height:100px;_height:100px;border:0;padding:3px;">
							<input type="hidden" name="photo" value="${user.photo}">
							<a href="javascript:void(0)" name="deleteImg">删除</a>
						</li>
					</c:if>
				</ol>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<%--<div class="control-group">
			<label class="control-label">归属公司:</label>
			<div class="controls">
                <sys:treeselect id="company" name="company.id" value="${user.company.id}" labelName="company.name" labelValue="${user.company.name}"
					title="公司" url="/sys/office/treeData?type=1" cssClass="required"/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">归属部门:</label>
			<div class="controls">
                <sys:treeselect id="office" name="office.id" value="${user.office.id}" labelName="office.name" labelValue="${user.office.name}"
					title="部门" url="/sys/office/treeData?type=2" cssClass="required" notAllowSelectParent="true"/>
			</div>
		</div>--%>
		<div class="control-group">
			<label class="control-label">姓名:</label>
			<div class="controls">
				<form:input path="name" htmlEscape="false" maxlength="50" class="required"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">登录名:</label>
			<div class="controls">
				<input id="oldLoginName" name="oldLoginName" type="hidden" value="${user.loginName}">
				<form:input path="loginName" htmlEscape="false" maxlength="50" class="required userName"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>

		<div class="control-group">
			<label class="control-label">密码:</label>
			<div class="controls">
				<input id="newPassword" name="newPassword" type="password" value="" maxlength="50" minlength="3" class="${empty user.id?'required':''}"/>
				<c:if test="${empty user.id}"><span class="help-inline"><font color="red">*</font> </span></c:if>
				<c:if test="${not empty user.id}"><span class="help-inline">若不修改密码，请留空。</span></c:if>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">确认密码:</label>
			<div class="controls">
				<input id="confirmNewPassword" name="confirmNewPassword" type="password" value="" maxlength="50" minlength="3" equalTo="#newPassword"/>
				<c:if test="${empty user.id}"><span class="help-inline"><font color="red">*</font> </span></c:if>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">邮箱:</label>
			<div class="controls">
				<form:input path="email" htmlEscape="false" maxlength="100" class="email"/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">电话:</label>
			<div class="controls">
				<form:input path="phone" htmlEscape="false" maxlength="100" class="phone"/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">手机:</label>
			<div class="controls">
				<form:input path="mobile" id="mobile" htmlEscape="false" maxlength="100" class="phone"/><font color="red">*</font>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">是否允许登录:</label>
			<div class="controls">
				<form:select path="loginFlag">
					<form:options items="${fns:getDictList('yes_no')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
				</form:select>
				<span class="help-inline"><font color="red">*</font> “是”代表此账号允许登录，“否”则表示此账号不允许登录</span>
			</div>
		</div>
		<%--<div class="control-group">
			<label class="control-label">用户类型:</label>
			<div class="controls">
				<form:select path="userType" class="input-xlarge">
					<form:option value="" label="请选择"/>
					<form:options items="${fns:getDictList('sys_user_type')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
				</form:select>
			</div>
		</div>--%>
		<div class="control-group">
			<label class="control-label">区域名称:</label>
			<div class="controls">
					<span><!--${user.areas}-->
						<select id="box" name="provinces" class="input-medium">
							<option>请选择</option>
							<c:forEach items="${proList}" var="pro">
								<c:choose>
									<c:when test="${user.provinces eq pro.province}">
										<option  value="${pro.provinceid}" selected >${pro.province}</option>
									</c:when>
									<c:otherwise>
										<option  value="${pro.provinceid}" >${pro.province}</option>
									</c:otherwise>
								</c:choose>

							</c:forEach>
						</select>
					</span>
				&nbsp;&nbsp;
				<span>
						<select id="box1" name='citys' class="input-medium">
							<c:if test="${user.citys !=null }">
								<option selected value="${area.cityId}">${user.citys}</option>
							</c:if>
							<option>请选择</option>
						</select>
					</span>
			<%--	<span>
					<select id="box2" name='areas' class="input-medium required">
                        <option>请选择</option>
						<c:if test="${user.areas !=null }">
							<option selected value="${area.areaId}">${user.areas}</option>
						</c:if>
					</select>
					</span>--%>
				<font color="red" id="agent" >*</font>
			<%--	&nbsp;&nbsp;
				<span>
					<select id="box2" name='areas' class="input-medium">
						<c:if test="${user.areas!=null}">
							<option value="${area.areaId}" selected>${user.areas}</option>
						</c:if>
						<option>请选择</option>
					</select>
					</span>--%>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">备注:</label>
			<div class="controls">
				<form:textarea path="remarks" htmlEscape="false" rows="3" maxlength="200" class="input-xlarge"/>
			</div>
		</div>
		<c:if test="${not empty user.id}">
			<div class="control-group">
				<label class="control-label">创建时间:</label>
				<div class="controls">
					<label class="lbl"><fmt:formatDate value="${user.createDate}" type="both" dateStyle="full"/></label>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label">最后登陆:</label>
				<div class="controls">
					<label class="lbl">IP: ${user.loginIp}&nbsp;&nbsp;&nbsp;&nbsp;时间：<fmt:formatDate value="${user.loginDate}" type="both" dateStyle="full"/></label>
				</div>
			</div>
		</c:if>
		<div class="form-actions">
				<input id="btnSubmit" class="btn btn-primary" type="button" value="保 存"/>&nbsp;
			<input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
		</div>
	</form:form>
</body>
</html>